﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace appLibrary
{
   public  class ImportExcel
    {

      


       public DataTable getImportExcel(string strSourceFile, string sheet, string user)
       {
           OleDbConnection connect = null;
           OleDbDataAdapter dap = null;
           try
           {
               string strconn = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
                                        @"Data Source=" + strSourceFile + @";Extended Properties=" + '"' + "Excel 8.0;HDR=YES ;imex=1" + '"'; 

               connect = new OleDbConnection(strconn);
               dap = new OleDbDataAdapter("Select * from [" + sheet + "$]", connect);
               DataTable dt = new DataTable();

               dap.Fill(dt);
               connect.Close();

               Log.eLog(user, "Se realizó la carga de datos de excel exitosamente.");
               return dt;

           }
           catch (Exception ex)
           {
               Log.eLog(user, "(ERROR): se originó errores al intentar conectar a Microsoft excel. " + ex.ToString());
               throw new Exception(ex.Message);

               

           }
           finally
           {
               if (connect != null)
                   connect.Close();
               if (dap != null)
                   dap.Dispose();

           }
           
          

       }


    }
}
